package com.xs.guess_egg_game;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    //定义对象
    private TextView text;
    private ImageView imageView1;
    private ImageView imageView2;
    private ImageView imageView3;
    //定义一个列表，存储三张图片的id
    List<Integer> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定控件
        imageView1 = (ImageView) findViewById(R.id.imageView1);
        imageView2 = (ImageView) findViewById(R.id.imageView2);
        imageView3 = (ImageView) findViewById(R.id.imageView3);
        Button button = (Button) findViewById(R.id.btn);
        text = (TextView) findViewById(R.id.textView1);
        //在列表中添加元素
        list.add(R.drawable.shoe_sorry);
        list.add(R.drawable.shoe_sorry);
        list.add(R.drawable.shoe_ok);
        Collections.shuffle(list);//随机显示

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击再玩一次，恢复原有标题和鞋子图片
                text.setText("猜猜鸡蛋在哪只鞋子里？");
                imageView1.setAlpha((float)1);//设置图片透明度位完全不透明
                imageView2.setAlpha((float)1);//设置图片透明度位完全不透明
                imageView3.setAlpha((float)1);//设置图片透明度位完全不透明
                Collections.shuffle(list);
                imageView1.setImageDrawable(getResources().getDrawable(R.drawable.shoe_default));//将imageview图片设置为默认图片
                imageView2.setImageDrawable(getResources().getDrawable(R.drawable.shoe_default));
                imageView3.setImageDrawable(getResources().getDrawable(R.drawable.shoe_default));
            }
        });
        //设置监听
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView1.setImageDrawable(getResources().getDrawable(list.get(0)));
                imageView2.setImageDrawable(getResources().getDrawable(list.get(1)));
                imageView3.setImageDrawable(getResources().getDrawable(list.get(2)));
                if(list.get(0)==R.drawable.shoe_ok){
                    imageView1.setAlpha((float)0.5);
                    imageView2.setAlpha((float)1);
                    imageView3.setAlpha((float)1);
                    text.setText("运气真好，猜对了！！");
                }else {
                    text.setText("猜错了，在玩一次？");
                }
            }
        });

        imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView1.setImageDrawable(getResources().getDrawable(list.get(0)));
                imageView2.setImageDrawable(getResources().getDrawable(list.get(1)));
                imageView3.setImageDrawable(getResources().getDrawable(list.get(2)));
                if(list.get(1)==R.drawable.shoe_ok){
                    imageView1.setAlpha((float)1);
                    imageView2.setAlpha((float)0.5);
                    imageView3.setAlpha((float)1);
                    text.setText("运气真好，猜对了！！");
                }else {
                    text.setText("猜错了，在玩一次？");
                }
            }
        });

        imageView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView1.setImageDrawable(getResources().getDrawable(list.get(0)));
                imageView2.setImageDrawable(getResources().getDrawable(list.get(1)));
                imageView3.setImageDrawable(getResources().getDrawable(list.get(2)));
                if(list.get(2)==R.drawable.shoe_ok){
                    imageView1.setAlpha((float)1);
                    imageView2.setAlpha((float)1);
                    imageView3.setAlpha((float)0.5);
                    text.setText("运气真好，猜对了！！");
                }else {
                    text.setText("猜错了，在玩一次？");
                }
            }
        });

    }
}
